/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sys.screen;

import java.io.IOException;
import java.sql.SQLException;
import java.text.NumberFormat;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import sys.bean.MovimentacaoBean;
import sys.dao.MovimentacaoDao;
import sys.utils.Mensagens;
import sys.utils.Utils;

/**
 *
 * @author Administrador
 */
public class UIValeDetalhes extends javax.swing.JDialog {

    NumberFormat formatoMoeda = NumberFormat.getCurrencyInstance();
    private double total = 0;

    public UIValeDetalhes(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        Utils.escClose(this);
        UIManager.put("Table.alternateRowColor", new java.awt.Color(230, 255, 255));
    }

    public void atualizaTabela(String data, String cliente) {

        DefaultTableModel modelo = (DefaultTableModel) tabela.getModel();
        modelo.setNumRows(0);

        try {
            for (MovimentacaoBean mov : MovimentacaoDao.listaProdutos(data, cliente)) {
                total += mov.getQuantidade() * mov.getValor();
                modelo.addRow(new Object[]{
                    mov.getProduto().getDescricao(), formatoMoeda.format(mov.getQuantidade()).replace("R$", ""), formatoMoeda.format(mov.getValor()).replace("R$", ""),
                    formatoMoeda.format(mov.getQuantidade() * mov.getValor()).replace("R$", "")
                });
            }
            txTotal.setText(formatoMoeda.format(total));
        } catch (SQLException | IOException ex) {
            Mensagens.msgErro("erro ao atualizar tabela\n" + ex.getMessage());
            ex.printStackTrace();
        }

    }

    public void atualizaTabelaPedido(String pedOrigem, String data) {

        DefaultTableModel modelo = (DefaultTableModel) tabela.getModel();
        modelo.setNumRows(0);

        try {
            for (MovimentacaoBean mov : MovimentacaoDao.listaProdutosPedido(pedOrigem, data)) {
                total += mov.getQuantidade() * mov.getValor();
                modelo.addRow(new Object[]{
                    mov.getProduto().getDescricao(), formatoMoeda.format(mov.getQuantidade()).replace("R$", ""), formatoMoeda.format(mov.getValor()).replace("R$", ""),
                    formatoMoeda.format(mov.getQuantidade() * mov.getValor()).replace("R$", "")
                });
            }
            txTotal.setText(formatoMoeda.format(total));
        } catch (SQLException | IOException ex) {
            Mensagens.msgErro("erro ao atualizar tabela\n" + ex.getMessage());
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txVale = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txData = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txVendedor = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txCliente = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        txTotal = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("DETALHES DO VALE MERCADORIA");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel1.setText("N. VALE:");

        txVale.setEditable(false);
        txVale.setBackground(new java.awt.Color(245, 245, 245));
        txVale.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        txVale.setForeground(new java.awt.Color(51, 51, 255));
        txVale.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txVale.setBorder(null);

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel2.setText("DATA:");

        txData.setEditable(false);
        txData.setBackground(new java.awt.Color(245, 245, 245));
        txData.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        txData.setForeground(new java.awt.Color(51, 51, 255));
        txData.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txData.setBorder(null);

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel3.setText("VENDEDOR:");

        txVendedor.setEditable(false);
        txVendedor.setBackground(new java.awt.Color(245, 245, 245));
        txVendedor.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        txVendedor.setForeground(new java.awt.Color(51, 51, 255));
        txVendedor.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txVendedor.setBorder(null);

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel4.setText("CLIENTE:");

        txCliente.setEditable(false);
        txCliente.setBackground(new java.awt.Color(245, 245, 245));
        txCliente.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        txCliente.setForeground(new java.awt.Color(51, 51, 255));
        txCliente.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txCliente.setBorder(null);

        tabela.setAutoCreateRowSorter(true);
        tabela.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        tabela.setForeground(new java.awt.Color(51, 102, 255));
        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "DESCRIÇÃO", "QTD", "UNT", "TOTAL"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setGridColor(new java.awt.Color(204, 204, 204));
        tabela.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tabela);
        if (tabela.getColumnModel().getColumnCount() > 0) {
            tabela.getColumnModel().getColumn(0).setPreferredWidth(300);
        }

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel5.setText("TOTAL:");

        txTotal.setEditable(false);
        txTotal.setBackground(new java.awt.Color(245, 245, 245));
        txTotal.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        txTotal.setForeground(new java.awt.Color(51, 51, 255));
        txTotal.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txTotal.setBorder(null);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txCliente)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(txVale, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addComponent(txData)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(221, 221, 221)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addGap(59, 59, 59))
                                .addComponent(txVendedor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addGap(59, 59, 59))
                                .addComponent(txTotal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txVale, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txVendedor, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txData, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(23, 23, 23)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(1, 1, 1)
                        .addComponent(txCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(23, 23, 23)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabela;
    public javax.swing.JTextField txCliente;
    public javax.swing.JTextField txData;
    public javax.swing.JTextField txTotal;
    public javax.swing.JTextField txVale;
    public javax.swing.JTextField txVendedor;
    // End of variables declaration//GEN-END:variables
}
